let http = require('axios')

module.exports = {
  getPageToken: function (appid, appsecret, code) {
    return new Promise((resolve, reject) => {
      http.get(`https://api.weixin.qq.com/sns/oauth2/access_token?appid=${appid}&secret=${appsecret}&code=${code}&grant_type=authorization_code`).then(res => {
        resolve(res.data)
      })
    })
  },
  refreshPageToken: function (appid, refreshToken) {
    return new Promise((resolve, reject) => {
      http.get(`https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=${appid}&grant_type=refresh_token&refresh_token=${refreshToken}`).then(res => {
        resolve(res.data)
      })
    })
  },
  getUserInfoByPageToken: function (pageToken, openId) {
    return new Promise((resolve, reject) => {
      http.get(`https://api.weixin.qq.com/sns/userinfo?access_token=${pageToken}&openid=${openId}&lang=zh_CN`).then(res => {
        resolve(res.data)
      })
    })
  },
  checkPageToken: function (pageToken, openId) {
    return new Promise((resolve, reject) => {
      http.get(`https://api.weixin.qq.com/sns/auth?access_token=${pageToken}&openid=${openId}`).then(res => {
        resolve(res.data)
      })
    })
  },
  getJsapiTicket: function (accessToken) {
    return new Promise((resolve, reject) => {
      http.get(`https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=${accessToken}&type=jsapi`).then(res => {
        resolve(res.data)
      })
    })
  }
}
